/*
 *  Copyright (C), 2015-2023
 *  FileName: fold_text
 *  Author: Tonight丶相拥
 *  Date: 3/7/23
 *  Description: 
 **/

part of appcommon;
// import 'package:flutter/gestures.dart';
// import 'package:flutter/material.dart';

class ExpandableText extends StatefulWidget {
  ExpandableText(this.text, {this.maxLines, this.style,
    bool expand = false, bool needFolding = false,
    this.expandTextStyle, this.foldingTextStyle, this.expandText = "展示",
    this.foldingText = "收起", this.onTap, this.onFolding})
      : this._expand = expand, this._needFolding = needFolding;
  /// 最大行数
  final int? maxLines;
  /// 显示数据
  final String text;
  /// 文字样式
  final TextStyle? style;
  /// 展示文字 样式
  final TextStyle? expandTextStyle;
  final TextStyle? foldingTextStyle;
  /// 最后显示文字
  final String expandText;
  final String foldingText;
  /// 是否展开
  final bool _expand;
  final bool _needFolding;
  /// 是否点击
  final void Function()? onTap;
  final void Function()? onFolding;
  @override
  createState() => _ExpandableTextState(text, style: style, maxLines: maxLines, expand: _expand,
      expandTextStyle: expandTextStyle ?? TextStyle(color: Color.fromARGB(255, 15, 203, 57),
          fontSize: 15,
          fontWeight: w_500
      ), expandText: expandText,
      onTap: onTap, onFolding: onFolding,
      needFolding: _needFolding,
      foldingText: foldingText,
      foldingTextStyle: foldingTextStyle ?? TextStyle(color: Color.fromARGB(255, 15, 203, 57),
          fontSize: 15,
          fontWeight: w_500
      )
  );
}

class _ExpandableTextState extends State<ExpandableText> {
  _ExpandableTextState(this.text,{this.maxLines, this.style,
    required bool expand, required bool needFolding, this.expandTextStyle,
    this.expandText, this.onTap, this.onFolding,
    this.foldingTextStyle, this.foldingText}): this._expand = expand,
        this._needFolding = needFolding;
  /// 最大行数
  final int? maxLines;
  /// 显示数据
  final String text;
  /// 文字样式
  final TextStyle? style;
  /// 是否展开
  bool _expand;
  /// 最后显示文字
  final String? expandText;
  final String? foldingText;
  /// 展示文字 样式
  final TextStyle? expandTextStyle;
  final TextStyle? foldingTextStyle;
  /// 是否点击
  final void Function()? onTap;
  final void Function()? onFolding;
  /// 是否需要折叠
  final bool _needFolding;

  @override
  initState(){
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (context, size) {
      /// 初始化想要显示的
      TextSpan span = TextSpan(text: text, style: style);
      /// 初始化 画笔
      TextPainter painter = TextPainter(text: span, maxLines: maxLines, textDirection: TextDirection.ltr);
      /// 布局
      painter.layout(maxWidth: size.maxWidth);
      /// painter 是否超出最大行数
      if (painter.didExceedMaxLines) {
        return _expand ? (_needFolding ? Text.rich(TextSpan(
            children: [
              TextSpan(text: text, style: style),
              const TextSpan(text: "\r\n"),
              TextSpan(text: foldingText, style: foldingTextStyle, recognizer: TapGestureRecognizer()..onTap = (){
                _expand = !_expand;
                setState(() {});
                if (onFolding != null){
                  onFolding?.call();
                }
              })
            ]
        )) : Text(text, style: style)) : Container(
          child: Stack(
            children: <Widget>[
              Text(text, maxLines: maxLines, overflow: TextOverflow.ellipsis, style: style),
              Positioned(right: 0, bottom: 0, child: Container(
                color: Colors.white,
                child: Text.rich(TextSpan(
                    children: [
                      TextSpan(text: "...", style: style),
                      TextSpan(text: expandText ?? "", style: expandTextStyle, recognizer: TapGestureRecognizer()..onTap = (){
                        _expand = !_expand;
                        setState(() {});
                        if (onTap != null)
                          onTap?.call();
                      })
                    ]
                )),
              ))
            ],
          ),
        );
      } else {
        return Text(text, style: style);
      }
    });
  }
}